export function getCurrentTime(dateDelimiter = '-', timeDelimiter = ': ') {

  var date = new Date();
  var db = function (d) {
    if (d < 10) {
      return "0" + d;
    }
    return d;
  };
  var y = date.getFullYear();
  var m = db(date.getMonth() + 1);
  var d = db(date.getDate());

  var h = date.getHours();
  var mt = date.getMinutes();
  var s = date.getSeconds();

  var weeks = ["日", "一", "二", "三", "四", "五", "六"];
  var w = date.getDay();

  var dt = y + dateDelimiter + m + dateDelimiter + d;
  var time = db(h) + timeDelimiter + db(mt) + timeDelimiter + db(s);
  var week = "星期" + weeks[w];

  return {
    date: dt,
    time: time,
    week: week
  }
}


export function sleep(ms) {
  return new Promise(resolve => {
    setTimeout(resolve, ms)
  })
}

export function randomBetween(start, end) {
  return Math.floor(Math.random() * (end - start + 1) + start)
}

export function formatNum(val) {
  return String(val).replace(/(\d)(?=(\d{3})+$)/g, "$1, ");
}

export function toRad(deg) {
  return deg * Math.PI / 180
}

export function toDeg(rad) {
  return rad * 180 / Math.PI
}

export function each(obj, func, ctx) {
  Object.keys(obj).forEach(key => {
    let value = obj[key];
    if (typeof func === 'function') {
      func.call(ctx, value, key);
    }
  });
}

export function mouseCapture(element) {

  var mouse = {
    x: 0,
    y: 0
  }

  element.addEventListener('mousemove', function (event) {
    var x = event.pageX;
    var y = event.pageY;

    var rect = element.getBoundingClientRect();

    x -= rect.x;
    y -= rect.y;

    mouse.x = x;
    mouse.y = y;

  })


  return mouse;
}

export function division(str) {
  if (!str || str.length < 3) return str;
  str = str.split("").reverse();
  let result = [],
      count = 0;
  while (count <= str.length - 1) {
      if (count !== 0 && count % 3 === 0) {
          result.push(",", str[count]);
      } else {
          result.push(str[count]);
      }
      count++;
  }
  return result.reverse().join("");
}
